package com.neusoft.neu24.nep.service.impl;

import com.neusoft.neu24.nep.entity.ComonResult;
import com.neusoft.neu24.nep.entity.Member;
import com.neusoft.neu24.nep.entity.ResultCode;
import com.neusoft.neu24.nep.mapper.MemberMapper;
import com.neusoft.neu24.nep.service.IMemberService;
import com.neusoft.neu24.nep.util.SmsCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.freemarker.FreeMarkerTemplateAvailabilityProvider;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * MemberServiceImpl asdfasdf
 *
 * @author Administrator
 * @version 2024/05/24 15:59
 **/
@Service
public class MemberServiceImpl  implements IMemberService {
    @Autowired
    MemberMapper mapper;


    /**
     * 获得所有的成员列表
     *
     * @return List<Member>
     */
    @Override
    public List<Member> selectAll() {
        List<Member> lms = mapper.selectAll();
        return lms;
    }

    /**
     * 成员登录业务，如果成功，范围Member对象
     *
     * @param logid
     * @param logpwd
     * @return Member对象
     */
    @Override
    public ComonResult<Member> login(String logid, String logpwd){
        if (logid.trim().length() <=3){
            return  ComonResult.failed( ResultCode.LOGIN_NAME_IS_EMPTY );
        }
        if (logpwd.trim().length() <=3){
            return  ComonResult.failed( ResultCode.LOGIN_PAWWWORD_IS_EMPTY );
        }

        Member member = mapper.selectByLogid(logid);
        if (member ==null){
            return ComonResult.failed(ResultCode.LOGIN_NAME_IS_NOT_REGISTERED);
        }

        if (member!=null &&  (!member.getLogpwd().equals(logpwd) )){
            return   ComonResult.failed(  ResultCode.LOGIN_PAWWWORD_ERROR );
        }
        return  ComonResult.success(member);
    }

    /**
     * 成员注册业务，如果成功，返回新成员的唯一ID，否则返回-1
     *
     * @param member
     * @return
     */
    @Override
    public ComonResult register(Member member) {
//        数据检查
//        id=null, mname=公众监督员1, logid=jdy1, logpwd=6666, tel=19818123853, gender=女, birthday=Fri Sep 25 08:00:00 CST 2020, roleid=null, state=null, role=null, projects=null
//        判断logid是否已经存在
        int  i =  mapper.selectCountByLogid(member.getLogid());
        if( i ==1){
            return  ComonResult.failed(ResultCode.LOGIN_NAME_IN_USE);
        }
        try
        {
            i = mapper.addMember(member);
            if (i==1){
//            返回的不是影响记录的函数，而是新增数据的自动标识列的值
                return  ComonResult.success(member.getId());
            } else {
                return  ComonResult.failed(ResultCode.INSERT_FAILED);
            }
        }catch (DataAccessException e){
            return  ComonResult.failed(500, e.getMessage());
        }
    }

    /**
     * 根据手机号实现监督员注册
     *
     * @param tel
     * @return
     */
    @Override
    public ComonResult<Member> register(String tel) {
//        数据检查
//        判断手机号是否已经存在
        int  i =  mapper.selectCountByTel(tel);
        if(i >0){
            return  ComonResult.failed(ResultCode.TEL_IN_USE);
        }
//        生成验证码
        String smscode = SmsCode.getCode();
        i =  mapper.addMemberWithTelSmsCode( tel,   smscode );
//        发送短信 ，使用kafka发短信
        if (i ==1){
            Member member =new Member();
            member.setTel(tel);
            member.setSmsCode(smscode);
            return    ComonResult.success(member);
        }
        return ComonResult.failed(500,"asdfasdf");
    }







    @Override
    public ComonResult<Member> loginm(String tel, String smscode) {
//     Calendar时间计算的工具
//        Calendar.getInstance() ：参考时间是系统的当前时间
        Calendar calendar = Calendar.getInstance();
        calendar.add( Calendar.MINUTE, -5  );
        Date now = calendar.getTime();
        Member member = mapper.selectMemberByTelAndSmsCode(tel, smscode, now );
        if (member ==null){
            return ComonResult.failed(ResultCode.LOGIN_FAILED_CODEERROR_TIMEOUT);
        }
        return ComonResult.success(member);
    }

    @Override
    public ComonResult  resetpwd(Member member) {
        int  i =  mapper.modMemberDsql(member);
        if (i ==1){
            return   ComonResult.success(  i  );
        }
        return ComonResult.failed( ResultCode.UPDATE_FAILUER );
    }
}